#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import os.path
import time
import unittest
from jslBaseInterface.jmp_box import JmpyBox
from jslBaseInterface.jmp_file import JmpyFile


class MyTestCase(unittest.TestCase):
    def setUp(self):
        self.test_png1 = r"TestPic\1746085454981.jpg"
        self.test_png2 = r"TestPic\1746085466373.jpg"
        self.test_png3 = r"TestPic\1746085476231.jpg"
        self.test_png4 = r"TestPic\1746085484739.jpg"
        self.test_png5 = r"TestPic\1746085526638.jpg"

    def testSomething(self):
        self.assertEqual(True, True)  # add assertion here

    def testClear(self):
        JmpyFile.jExecuteScript(
            JmpyFile.jCloseAll(),
            scrip_name="clear.jsl"
        )

    def testNewWindow(self):
        print(JmpyBox.jNewWin(
            "A", "B"
        ))

    def testOpenPng(self):
        """
        读取图片
        用于分解Python如何调用Jmp读取一份Png文件
        :return:
        """
        JmpyFile.jExecuteScript(
            JmpyFile.jOpenPng(self.test_png1)
        )

    def testLoadPngPlotWindows(self):
        """
        打开一个Png文件, 并用一个Window去装载这个Png
        :return:
        """
        bp = "bp"
        bp2 = "bp2"
        script = [
            JmpyFile.jLoadJpg(self.test_png1, pb_name=bp),
            JmpyFile.jLoadJpg(self.test_png2, pb_name=bp2),
            JmpyBox.jNewWin(bp, bp2, title="打开了二个图片"),
        ]
        JmpyFile.jExecuteScript(
            *script
        )

    def testLoadPngPlotOutlineBox(self):
        """
        打开一个Png文件,
        并用一个Window去装载这个Png
        另外这个Png外面还加一个外框来包裹, 并命名一个标题
        :return:
        """
        bp = "bp"
        script = [
            JmpyFile.jLoadJpg(self.test_png1, pb_name=bp),
            JmpyBox.jNewWin(
                JmpyBox.jOutlineBox(bp, title="外框Title")
                , title="打开了一个图片"
            )
        ]
        JmpyFile.jExecuteScript(
            *script
        )

    def testLoadPngHListBox(self):
        """
        打开一个Png文件放到Box中, 放到一边
        准备一个新的Window
        在Window中放入一个HListBox
        然后在HListBox中放入三个Png的Box
        :return:
        """
        bp = "bp"
        bp2 = "bp2"
        bp3 = "bp3"
        script = [
            JmpyFile.jLoadJpg(self.test_png1, pb_name=bp),
            JmpyFile.jLoadJpg(self.test_png2, pb_name=bp2),
            JmpyFile.jLoadJpg(self.test_png3, pb_name=bp3),
            JmpyBox.jNewWin(
                JmpyBox.jHListBox(bp, bp2, bp3)
                , title="打开了三个图片并横向排列在一起")
        ]
        JmpyFile.jExecuteScript(
            *script
        )

    def testLoadPngVListBox(self):
        """
        打开一个Png文件放到Box中, 放到一边
        准备一个新的Window
        在Window中放入一个VListBox
        然后在VListBox中放入三个Png的Box
        :return:
        """
        bp = "bp"
        bp3 = "bp3"
        script = [
            JmpyFile.jLoadJpg(self.test_png1, pb_name=bp),
            JmpyFile.jLoadJpg(self.test_png3, pb_name=bp3),
            JmpyBox.jNewWin(
                JmpyBox.jVListBox(bp3, bp, bp)
                , title="打开了两个图片, 但是有一个图片放入VListBox两次, 所以是三张图片垂直排列在一起")
        ]
        JmpyFile.jExecuteScript(
            *script
        )

    def testLoadPngHVListBox(self):
        """
        打开一个Png文件放到Box中, 放到一边
        准备一个新的Window
        在Window中放入一个HListBox
        然后在VListBox中放入三个Png的Box
        :return:
        """
        bp = "bp"
        script = [
            JmpyFile.jLoadJpg(self.test_png1, pb_name=bp),
            JmpyBox.jNewWin(
                JmpyBox.jVListBox(
                    bp,
                    JmpyBox.jHListBox(bp, bp),
                    bp
                )
                , title="打开了一个图片, 并引用了四次, 进行排列")
        ]
        JmpyFile.jExecuteScript(
            *script
        )

    def testLoadPngPlotGroupListBox(self):
        """
        打开一个Png文件放到Box中, 放到一边
        准备一个新的Window
        在Window中放入一个VListBox
        然后在VListBox中放入三个Png的Box
        :return:
        """
        bp = "bp"
        bp2 = "bp2"
        bp3 = "bp3"
        bp4 = "bp4"
        bp5 = "bp5"
        rbiv = "rbiv"
        # save_html_path = r"D:\testLoadPngPlotGroupListBox.html"
        bp_box = JmpyBox.jOutlineBox(bp, title="Wafer")
        bp_box2 = JmpyBox.jOutlineBox(bp2, title="Wafer2")
        bp_box3 = JmpyBox.jOutlineBox(bp3, title="Wafer3")
        bp_box4 = JmpyBox.jOutlineBox(bp4, title="Wafer4")
        bp_box5 = JmpyBox.jOutlineBox(bp5, title="Wafer5")
        scripts = [
            JmpyFile.jLoadJpg(self.test_png1, pb_name=bp),
            JmpyFile.jLoadJpg(self.test_png2, pb_name=bp2),
            JmpyFile.jLoadJpg(self.test_png3, pb_name=bp3),
            JmpyFile.jLoadJpg(self.test_png4, pb_name=bp4),
            JmpyFile.jLoadJpg(self.test_png5, pb_name=bp5),
            JmpyBox.jNewWin(
                JmpyBox.jGroupBox(bp_box, bp_box2, bp_box3, bp_box4, bp_box5),
                title="GroupPlot", rbiv=rbiv
            ),
            # JmpFile.jSaveInteractiveHtml(save_html_path, rbiv)
        ]
        JmpyFile.jExecuteScript(
            *scripts
        )

    def testLoadPngPlotGroupListBoxSaveHtml(self):
        """
        打开一个Png文件放到Box中, 放到一边
        准备一个新的Window
        在Window中放入一个VListBox
        然后在VListBox中放入三个Png的Box
        :return:
        """
        bp = "bp"
        rbiv = "rbiv"
        save_html_path = r"D:\testLoadPngPlotGroupListBoxSaveHtml.html"
        scripts = [
            JmpyFile.jLoadJpg(self.test_png1, pb_name=bp),
            JmpyBox.jNewWin(
                JmpyBox.jGroupBox(bp, bp, bp, bp, bp),
                title="GroupPlot", rbiv=rbiv
            ),
            JmpyFile.jSaveInteractiveHtml(save_html_path, rbiv)
        ]
        JmpyFile.jExecuteScript(
            *scripts
        )
        times = 0
        while not os.path.exists(save_html_path):
            if times > 5:
                raise Exception("生成PDF文件失败")
            times += 1
            time.sleep(2)

    def testLoadPngPlotGroupListBoxSavePng(self):
        """
        打开一个Png文件放到Box中, 放到一边
        准备一个新的Window
        在Window中放入一个VListBox
        然后在VListBox中放入三个Png的Box
        :return:
        """
        bp = "bp"
        rbiv = "rbiv"
        save_pdf_path = r"TestSwap\testLoadPngPlotGroupListBoxSavePdf.png"
        scripts = [
            JmpyFile.jLoadJpg(self.test_png1, pb_name=bp),
            JmpyBox.jNewWin(
                JmpyBox.jGroupBox(bp, bp, bp, bp, bp),
                title="testLoadPngPlotGroupListBoxSavePng", rbiv=rbiv
            ),
            JmpyFile.jSavePng(save_pdf_path, rbiv)
        ]
        JmpyFile.jExecuteScript(
            *scripts
        )
        time.sleep(2)
        times = 0
        while not os.path.exists(save_pdf_path):
            if times > 5:
                raise Exception("生成PNG文件失败")
            times += 1
            time.sleep(2)
        os.startfile(save_pdf_path)

    def testLoadPngPlotGroupListBoxSavePdf(self):
        """
        打开一个Png文件放到Box中, 放到一边
        准备一个新的Window
        在Window中放入一个VListBox
        然后在VListBox中放入三个Png的Box
        :return:
        """
        bp = "bp"
        rbiv = "rbiv"
        save_pdf_path = r"TestSwap\testLoadPngPlotGroupListBoxSavePdf.pdf"
        scripts = [
            JmpyFile.jLoadJpg(self.test_png1, pb_name=bp),
            JmpyBox.jNewWin(
                JmpyBox.jGroupBox(bp, bp, bp, bp, bp),
                title="testLoadPngPlotGroupListBoxSavePdf", rbiv=rbiv
            ),
            JmpyFile.jSavePdf(save_pdf_path, rbiv)
        ]
        JmpyFile.jExecuteScript(
            *scripts
        )
        time.sleep(2)
        times = 0
        while not os.path.exists(save_pdf_path):
            if times > 5:
                raise Exception("生成PDF文件失败")
            times += 1
            time.sleep(2)
        os.startfile(save_pdf_path)


if __name__ == '__main__':
    unittest.main()
